package com.sgb.kjwl.view.ui.fragment.shop;

import androidx.databinding.DataBindingUtil;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.sgb.kjwl.AppFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentShopPriceBinding;
import com.sgb.kjwl.di.self.DaggerSelfFragComponent;
import com.sgb.kjwl.di.self.SelfFragModule;
import com.sgb.kjwl.model.entity.shopping.ShopMyPriceListEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.swgk.core.event.EventConstant;
import com.sgb.link.event.LeaseEvent;
import com.sgb.kjwl.view.ui.adapter.shopping.ShopMyPriceAdapter;
import com.sgb.kjwl.viewmodel.shopping.ShopMyPriceModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.xrecyclerview.XRecyclerView;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import java.util.List;
import javax.inject.Inject;

/**
 * 我的报价列表界面
 */
public class ShopMyPriceFragment  extends AppFragment {
    public static final String DATATYPE = "data_type";
    private int dataType =1;

    private ShopMyPriceAdapter shopmypriceadapter;
    private FragmentShopPriceBinding binding;
    private boolean isLoadMore = false;
    @Inject
    ShopMyPriceModel shopmodel;
    private int page = 1;
    private  String searchShopPricename= "";
    private List<ShopMyPriceListEntity> shopEntityList;
    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerSelfFragComponent.builder()
                .appComponent(appComponent)
                .selfFragModule(new SelfFragModule(this))
                .build()
                .inject(this);
    }

    public static ShopMyPriceFragment newInstance(int type) {
        ShopMyPriceFragment fragment = new ShopMyPriceFragment();
        Bundle bundle = new Bundle();
        bundle.putInt(DATATYPE, type);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_shop_price, container, false);
        binding.setViewModel(shopmodel);
        return binding.getRoot();
    }
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
            if (!EventBus.getDefault().isRegistered(this)) {
                EventBus.getDefault().register(this);
            }
            if (getArguments() != null) {
                dataType = getArguments().getInt(DATATYPE);
            }
            initlistdata();

    }

    @Override
    public void onResume() {
        super.onResume();
        getdata();
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onEvent(LeaseEvent event) {
        if (event.getMessageTag() == EventConstant.EVENT_SEARCHSHOPPRICE) {
            searchShopPricename = (String) event.getMessageData();
            EventBus.getDefault().removeStickyEvent(event);
        }
        getdata();
    }

    private void initlistdata() {
        if (shopmypriceadapter ==null){
        shopmypriceadapter = new ShopMyPriceAdapter(getActivity(), shopEntityList);
        }
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        binding.xrv.setLayoutManager(layoutManager);
        binding.xrv.setAdapter(shopmypriceadapter);
        binding.xrv.setLoadingMoreEnabled(true);
        binding.xrv.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                getdata();
            }
            @Override
            public void onLoadMore() {
                isLoadMore = true;
                page = page + 1;
                shopmodel.getDataList(dataType,searchShopPricename, page);
            }
        });
        shopmypriceadapter.setOnItemClick(new AdapterOnItemClick<ShopMyPriceListEntity>() {
            @Override
            public void onItemClick(ShopMyPriceListEntity shopMyPriceListEntity, int position) {
               shopmodel.getDataInfo(shopMyPriceListEntity.getNo());
            }
        });
    }
        //获取数据信息
    private void getdata() {
        isLoadMore = false;
        page = 1;
        shopmodel.getDataList(dataType,searchShopPricename, page);
    }

    //返回的数据列表
    public void getShopMyPriceDataList(ListResponse<List<ShopMyPriceListEntity>> data) {
        if (data == null) {
            return;
        }
        List<ShopMyPriceListEntity> shopEntityList = data.getList();
        if (isLoadMore) {
            binding.xrv.loadMoreComplete();
            if (shopEntityList.size() > 0) {
                shopmypriceadapter.addDatas(shopEntityList);
            } else {
                binding.xrv.setLoadingMoreEnabled(false);
            }

        } else {
            binding.xrv.refreshComplete();
            if (shopEntityList.size() > 0) {
                binding.xrv.setVisibility(View.VISIBLE);
                binding.empty.emptyView.setVisibility(View.GONE);
                shopmypriceadapter.setDatas(shopEntityList);
                binding.xrv.setLoadingMoreEnabled(true);
            } else {
                shopmypriceadapter.setDatas(null);
                binding.xrv.setVisibility(View.GONE);
                binding.empty.emptyView.setVisibility(View.VISIBLE);
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

}